package org.molamil.acto.core.registry {
	import flash.utils.Dictionary;

	/**
	 * @author Jorge Hernandez
	 */
	public class TargetRegistry {
		
		
		// PROPERTIES
		
		protected var registry:Dictionary;
		
		
		// CONSTRUCTOR
		
		public function TargetRegistry() {
			registry = new Dictionary(true);
		}
		
		
		// PUBLIC METHODS
		
		public function registerTarget(id:String, target:Object):RegisteredTarget {
			var registeredTarget:RegisteredTarget = new RegisteredTarget();
			registeredTarget.id = id;
			registeredTarget.created = new Date();
			registry[target] = registeredTarget;
			return registeredTarget;
		}

		public function unregisterTarget(target:Object):Boolean {
			var b:Boolean = false;
			if (registry[target] != null) {
				delete registry[target];
				b = true;
			}
			return b;
		}
		
		public function unregisterTargetById(id:String):Boolean {
			var b:Boolean = false;
			for (var target:Object in registry) {
				if (registry[target].id == id) {
					delete registry[target];
					b = true;
					break;
				}
			}
			return b;
		}
		
		public function getRegisteredTarget(target:Object):RegisteredTarget {
			return registry[target];
		}

		public function getTargetById(id:String):Object {
			var foundTarget:Object;
			for (var target:Object in registry) {
				if (registry[target].id == id) {
					foundTarget = target;
					break;
				}
			}
			return foundTarget;
		}
		
		
	}
	
}
